import { ChainService } from '@/core/chain.service';
import { LogService } from '@/core/log.service';
import { PrismaService } from '@/core/prisma.service';
import { RedlockService } from '@/core/redlock.service';
import { InjectRedis } from '@/decorators';
import { Inject } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Redis } from 'ioredis';
import { halt } from "@/utils";

export abstract class BaseController {
  @Inject(ConfigService)
  protected readonly config: ConfigService;

  @Inject(PrismaService)
  protected readonly prisma: PrismaService;

  @Inject(RedlockService)
  protected readonly redlock: RedlockService;

  @Inject(LogService)
  protected readonly log: LogService;

  @Inject(ChainService)
  protected readonly chain: ChainService;

  @InjectRedis()
  protected readonly redis: Redis;

  async redisLock(
    keyName: string,
    uid: number,
    expire = 5,
    msg = '操作太快，请稍后重试!',
  ) {
    const lockKey = `${keyName}:${uid}`;
    if (await this.redis.get(lockKey)) {
      return halt(msg);
    }
    await this.redis.setex(lockKey, expire, 1);
  }
}
